# -*- coding: utf-8 -*-

# 僵尸游戏-骷髅游戏

import sys, time, random, math, pygame
from pygame.locals import *
from MyLibrary import *
from datetime import datetime, date, time
import itertools

reload(sys)
sys.setdefaultencoding('utf-8')


def calc_velocity(direction, vel=2.0):
    velocity = Point(0, 0)
    if direction == 0:  # down
        velocity.y = vel
    elif direction == 1:  # left
        velocity.x = -vel
    elif direction == 2:  # right
        velocity.x = vel
    elif direction == 3:  # up
        velocity.y = -vel
    return velocity


def reverse_direction(sprite):
    if sprite.direction == 0:  # down
        sprite.direction = 3
    elif sprite.direction == 1:  # left
        sprite.direction = 2
    elif sprite.direction == 2:  # right
        sprite.direction = 1
    elif sprite.direction == 3:  # up
        sprite.direction = 0

screen_width = 800
screen_height = 600

pygame.init()
winstyle = 0  # |FULLSCREEN
bestdepth = pygame.display.mode_ok((screen_width, screen_height), winstyle, 32)
screen = pygame.display.set_mode((screen_width, screen_height), winstyle, bestdepth)

pygame.display.set_caption("骷髅游戏")
font1 = pygame.font.Font("mini.ttf", 20)
framerate = pygame.time.Clock()

player_group = pygame.sprite.Group()
zombie_group = pygame.sprite.Group()
health_group = pygame.sprite.Group()

# 玩家
player = MySprite()
player.load("player.png", 50, 50, 5)
player.first_frame = 0
player.last_frame = 5
player.position = screen_width//2, screen_height//2
player_group.add(player)

# 骷髅
zombie_image = pygame.image.load("zombie.png").convert_alpha()
for n in range(0, 10):
    zombie = MySprite()
    zombie.load("zombie.png", 50, 50, 5)
    zombie.position = random.randint(0, 700), random.randint(0, 500)
    zombie.direction = random.randint(0, 3)
    zombie_group.add(zombie)

# 生命
health = MySprite()
health.load("health.png", 32, 32, 1)
health.position = 400, 300
health_group.add(health)

game_over = False
player_moving = False
player_health = 100

while True:
    framerate.tick(30)
    ticks = pygame.time.get_ticks()

    for event in pygame.event.get():
        if event.type == QUIT: sys.exit()

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]: sys.exit()
    elif keys[K_RETURN]:
        if game_over:
            game_over = False
            player_health = 100

    elif keys[K_UP] or keys[K_w]:
        player.direction = 3
        player_moving = True
    elif keys[K_RIGHT] or keys[K_d]:
        player.direction = 2
        player_moving = True
    elif keys[K_LEFT] or keys[K_a]:
        player.direction = 1
        player_moving = True
    elif keys[K_DOWN] or keys[K_s]:
        player.direction = 0
        player_moving = True
    else:
        player_moving = False

    # 帧范围
    if not game_over:
        player.first_frame = player.direction * player.columns
        player.last_frame = player.first_frame + player.columns - 1

        # 移动速度
        if not player_moving:
            player.frame = player.first_frame = player.last_frame
        else:
            player.velocity = calc_velocity(player.direction, 3.5)
            player.velocity.x *= 1.5
            player.velocity.y *= 1.5

        player_group.update(ticks, 50)

        # 边界检测
        if player_moving:
            player.X += player.velocity.x
            player.Y += player.velocity.y
            if player.X < 0: player.X = 0
            elif player.X > screen_width-50: player.X = screen_width-50
            if player.Y < 0: player.Y = 0
            elif player.Y > screen_height-50: player.Y = screen_height-50

        zombie_group.update(ticks, 50)

        # 移动僵尸
        for z in zombie_group:
            z.first_frame = z.direction * z.columns
            z.last_frame = z.first_frame+z.columns-1
            if z.frame < z.first_frame:
                z.frame = z.first_frame

            z.velocity = calc_velocity(z.direction)

            z.X += z.velocity.x
            z.Y += z.velocity.y
            if z.X < 0 or z.X > screen_width-50 or z.Y < 0 or z.Y > screen_width-50:
                reverse_direction(z)

        # 碰撞检测
        attacker = None
        attacker = pygame.sprite.spritecollideany(player, zombie_group)

        if attacker != None:
            if pygame.sprite.collide_rect_ratio(0.5)(player, attacker):
                player_health -= 10
                if attacker.X < player.X:
                    attacker.X -= 30
                elif attacker.X > player.X:
                    attacker.X += 30

        health_group.update(ticks, 50)

        # 吃药
        if pygame.sprite.collide_rect_ratio(0.5)(player, health):
            player_health += 30
            if player_health > 100: player_health = 100
            health.X = random.randint(0, 700)
            health.Y = random.randint(0, 500)

        if player_health <= 0:
            game_over = True
            player_health = 0

    # bg
    screen.fill((50, 50, 100))

    health_group.draw(screen)
    zombie_group.draw(screen)
    player_group.draw(screen)

    # 生命条
    pygame.draw.rect(screen, (50, 150, 50, 180), Rect(300, 570, player_health*2, 25))
    pygame.draw.rect(screen, (100, 200, 100, 180), Rect(300, 570, 200, 25), 2)

    if game_over:
        print_text(font1, 350, 100, u"游戏结束")
        print_text(font1, 300, 300, u"按回车键重新开始游戏")

    pygame.display.update()



